<template>
  <el-card>
    <template #header>
      <h3>修改密码</h3>
    </template>
    <div>
      <el-form
        :model="accountForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码" prop="oldpassword">
          <el-input
            type="password"
            v-model="accountForm.oldpassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newpassword">
          <el-input
            type="password"
            v-model="accountForm.newpassword"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="getsure">
          <el-input
            type="password"
            v-model="accountForm.getsure"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >确认更改</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { resetPwd } from '../../api/users'
// 引入resetPwd
// import { resetPwd } from '@/api/users.js'
export default {
  data() {
    var validateoldword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入原密码'))
      } else {
        callback()
      }
    }
    var validatenewword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else {
        // 表单的一致性规定，在验证新密码时失去焦点会触发确认密码的校验
        if (this.accountForm.getsure !== '') {
          this.$refs.ruleForm.validateField('getsure')
        }
        callback()
      }
    }
    var validategetsure = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.accountForm.newpassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      accountForm: {
        oldpassword: '',
        newpassword: '',
        getsure: ''
      },
      rules: {
        oldpassword: [{ validator: validateoldword, trigger: 'blur' }],
        newpassword: [{ validator: validatenewword, trigger: 'blur' }],
        getsure: [{ validator: validategetsure, trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // const token = localStorage.getItem('token')
          const users = JSON.parse(localStorage.getItem('users') || '{}')
          const { code, msg } = await resetPwd({
            member_id: users.member_id,
            access_token: users.access_token,
            oldPassword: this.accountForm.oldpassword,
            password: this.accountForm.newpassword,
            repassword: this.accountForm.getsure
          })
          if (code === 1) {
            this.$message.success(msg)
            localStorage.clear()
            // 修改完密码后要重新登录，就要清除掉浏览器里的登录缓存
            setTimeout(() => {
              // this.$router.push('/users/list')
              location.reload()
            }, 500)
          } else {
            this.$message.error(msg)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

//
<style lang="less" scoped>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}
.el-card {
  height: 100%;
  border: 0;
  .el-form {
    .el-form-item {
      padding: 10px;
    }
  }
  h3 {
    margin-left: 30px;
    font-size: 24px;
    font-weight: normal;
    letter-spacing: 2px;
  }
  .el-input {
    width: 300px;
  }
  .el-select {
    width: 300px;
  }
}
</style>
